PACKAGE BODY TASO IS

   function Creer return TAS IS
   BEGIN
      return new term'(Selon => Creer);
   END Creer;

   function Mettre(T: TAS; x:OBJET) return TAS IS
   BEGIN
      return new term'(selon => Mettre, Changed => T, Change => x);
   END Mettre;

   function Retirer(T : TAS; x : OBJET) return TAS IS
      Pragma Assert (Dans(T,x));
   BEGIN
      --Forcement un selon = mettre
      IF T.all.change = x THEN
         return T.all.changed;
      ELSE
         return mettre(T => Retirer(T.all.changed,x),
                       x => T.all.change);
      END IF;
   END Retirer;

   function Dans(T : TAS; x: OBJET) return BOOLEAN IS
   BEGIN
      IF T.all.selon = Creer THEN
         return false;
      ELSE
         IF T.all.selon = Mettre THEN
            IF T.all.change = x THEN
               return true;
            ELSE
               return Dans(T.all.changed, x);
            END IF;
         ELSE
            return false;
         END IF;
      END IF;

   END Dans;

   function Taille(T : TAS) return NATURAL IS
   BEGIN
      IF T.all.selon = Creer THEN
         return 0;
      ELSE
         return Taille(T.all.changed) + 1;
      END IF;
   END Taille;

   Function Ieme(T : TAS; i : NATURAL) return OBJET IS
      Pragma Assert (1 <= i AND i <= taille(T));
   BEGIN
      IF i <= Taille(T.all.changed) THEN
         IF ieme(T.all.changed,i) <= T.all.change THEN
            return ieme(T.all.changed,i);
         ELSE
            IF i = 1 THEN
               return T.all.change;
            ELSE
               IF ieme(T.all.changed,i-1) <= T.all.change THEN
                  return T.all.change;
               ELSE
                  return Ieme(T.all.changed,i-1);
               END IF;
            END IF;
         END IF;
      ELSE
         IF i = 1 THEN
            return T.all.change;
         ELSE
            IF ieme(T.all.changed, i-1) <= T.all.change THEN
               return T.all.change;
            ELSE
               return ieme(T.all.changed, i-1);
            END IF;
         END IF;
      END IF;
   END Ieme;

END TASO;
